package com.atguigu.demojuc.chap09;

public class VolatileDemo222 {

    // volatile  保证  JMM  可见性
    private Integer flag = 1; //共享变量

    public void a() {
        System.out.println("a - flag = " + flag);
        while (flag == 1) {

        }
        System.out.println("a2 - flag = " + flag);
    }

    public void b() {
        System.out.println("b - flag = " + flag);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        flag = 5;
        System.out.println("b2 - flag = " + flag);
    }

    public static void main(String[] args) throws InterruptedException { //main线程
        VolatileDemo222 volatileDemo222 = new VolatileDemo222();
        new Thread(() -> {
            volatileDemo222.a();
        }, "A").start();

        new Thread(() -> {
            volatileDemo222.b();
        }, "B").start();
    }
}